In this example, we will learn how to parse or read a JSON object using PHP built-in function.
JSON
Let’s create a separate file for the JSON.
Copy-paste the following line of the code and save the file as “test.json”
{"a":"apple","b":"banana","c":"carrot"}
PHP
PHP provides two variants of json_decode to parse or read the JSON.
Parse JSON data using PHP
json_decode($json_string) json_decode($json_string, true);
The complete code is as follows.
<?php $json_string = file_get_contents("test.json"); $json_arry = json_decode($json_string, true); foreach ($json_arry as $key => $value) echo $key . " -> ". $value. "<br/>"; ?>
Comments